<style lang="less">
@import "./login.less";
</style>

<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div style="position: absolute; width: 100%; height:100vh;">
            <video src="https://yxys.io/plugin.mp4" autoplay loop preload style="width: 100%; height: 100vh; object-fit: cover"/>
        </div>
        <div class="login-bg"></div>
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    医疗设备工况管理系统
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="userName">
                            <Input v-model="form.userName" placeholder="请输入用户名">
                                <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                                <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>登录</Button>
                        </FormItem>
                    </Form>
                    <p class="login-tip">请联系<a href="mailto:service@yingxuys.com?subject=%e7%94%b3%e8%af%b7%e5%8c%bb%e7%96%97%e8%ae%be%e5%a4%87%e5%b7%a5%e5%86%b5%e7%ae%a1%e7%90%86%e7%b3%bb%e7%bb%9f%e7%94%a8%e6%88%b7%e5%90%8d%e5%af%86%e7%a0%81">盈虚有数</a>获取用户名和密码</p>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
import Cookies from 'js-cookie';
import axios from 'axios';
import md5 from 'blueimp-md5';
export default {
    data () {
        return {
            form: {
                userName: '医芯智联',
                password: ''
            },
            rules: {
                userName: [
                    { required: true, message: '账号不能为空', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        handleSubmit () {
            this.$refs.loginForm.validate(async (valid) => {
                if (valid) {
                    try{
                        const tmpTable = await this.$getRemoteData(`${this.$mapServer}/equip/services/auth/login`,
                            { method: "post", data: {username: this.form.userName, password: md5(this.form.password)} }
                        );
                        if (!!tmpTable.data.auth_token){
                            sessionStorage.setItem('token', tmpTable.data.auth_token);
                            axios.defaults.headers.common['auth_token'] = tmpTable.data.auth_token;
                            this.$store.commit('setAvator', 'https://yxys.io/smart-plugin/avatar.jpg');
                            Cookies.set('user', this.form.userName);
                            this.$router.push({ name: 'management-device' });
                        }
                    }
                    catch(e){
                        if (e.response.status===401){
                            this.$Message.error('用户名或密码错误,请重新输入。');
                        }
                    }
                }
            });
        }
    }
};
</script>

<style>

</style>
